var marker = null;
var map = null;
var domain = document.domain;
window._AMapSecurityConfig = {
    serviceHost: 'http://map.sudayijia.com/_AMapService',
}
function loadScript(url, callback) {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    // 处理IE
    if (script.readyState) {
        script.onreadystatechange = function () {
            if (script.readyState === 'loaded' || script.readyState === 'complete') {
                script.onreadystatechange = null;
                callback();
            }
        }
    } else {
        // 处理其他浏览器的情况
        script.onload = function () {
            callback();
        }
    }
    script.src = url;
    document.body.append(script);
}

// 动态加载js
loadScript(`https://webapi.amap.com/maps?v=1.4.8&key=${mapkey}&plugin=AMap.Autocomplete,AMap.PlaceSearch`, function () {
    console.log('加载完成');
})
window.onload = function() {
    map.plugin(["AMap.ToolBar"], function() {
        map.addControl(new AMap.ToolBar());
    });
    if(location.href.indexOf('&guide=1')!==-1){
        map.setStatus({scrollWheel:false})
    }
}
window.onload = function () {
    //地图加载
    map = new AMap.Map("container", {
        zoom: 15,    //地图缩放级别
        resizeEnable: true,
    });
    var mylgt = $('#long').val()
    var mylat = $('#lat').val()
    //定义初始经纬度
    if (mylgt&&mylat){
        markerDi(mylgt,mylat)
    }
    map.on('click',function (e) {
        console.log(e)
        markerDi(e.lnglat.lng, e.lnglat.lat)
    })
    //输入地点提示
    var auto = new AMap.Autocomplete({
        input: "tipinput"
    });
    //构造地点查询类
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });
    //注册监听，当选中某条记录时会触发
    AMap.event.addListener(auto, "select", select);

    function select(e) {
        console.log(e)
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name);  //关键字查询查询
        if (e.poi.location){
            markerDi(e.poi.location.lng, e.poi.location.lat)
        }

    }
}

function markerDi(lng, lat) {

    if (marker) {
        marker.setMap(null);
        marker = null;
    }
    //重设标记
    marker = new AMap.Marker({
        icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png",
        position: [lng, lat],
        extData: {
            "markerLng": lng,
            "markerLat": lat,
        }
    });
    map.setZoomAndCenter(14, [lng, lat]);
    marker.setMap(map);
    getPositionByLonLats(lng, lat)
}

function getPositionByLonLats(lng, lat) {
    var lnglatXY = [lng, lat];// 地图上所标点的坐标
    AMap.service('AMap.Geocoder', function () {// 回调函数
        geocoder = new AMap.Geocoder({});
        geocoder.getAddress(lnglatXY, function (status, result) {
            if (status=='complete'&&result.info=='OK'){
                var province = result.regeocode.addressComponent.province;
                var city = result.regeocode.addressComponent.city;
                var county = result.regeocode.addressComponent.district;
                var street = result.regeocode.addressComponent.township;
                var address = result.regeocode.addressComponent.street+result.regeocode.addressComponent.streetNumber;
                $('#province').val(province)
                $('#city').val(city)
                $('#county').val(county)
                $('#street').val(street)
                $('#address').val(address)
                $('#long').val(lng)
                $('#lat').val(lat)
                $('#addr').html("解析地址："+province+' '+city+' '+county+' '+street+' '+address)
            }else {
                alert('经纬度解析失败')
            }
        });
    });
}

